home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / rotatestring / rotatestring.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.1 KB  |  160 lines

  1. /*
  2.     File:        RotateString.c
  3.  
  4.     Contains:    This function will return a BitMap that contains the
  5.                 text passed rotated 90°
  6.     
  7.                 The font characteristics are copied from
  8.                 the current port before creating the BitMap.
  9.     
  10.  
  11.     Written by: Randy Theland and Brigham Stevens    
  12.  
  13.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  25.                 
  26.                 8/27/92        Brigham            added a direction parameter to the end. It will 
  27.                                             rotate in the direction you specify. ALSO fixed 
  28.                                             some LAME bugs, such as the rotate routine did not 
  29.                                             preserve the current port,and it also did not dispose 
  30.                                             of a temporary bitmap it used.  Should be in good
  31.                                             shape now.
  32.  
  33. */
  34. #include <Memory.h>
  35. #include "RotateString.h"
  36.  
  37. /*
  38.     In pascal this is:
  39.  
  40.  function RotateString (str: Str255; var destMap: BitMap, direction : integer): OSErr;
  41.  
  42. */
  43.  
  44.  
  45. pascal OSErr RotateString( Str255 str, BitMap *destMap, short direction)
  46. {
  47.     GrafPort    srcPort;
  48.     char        *srcPtr, *destPtr;
  49.     FontInfo    f;
  50.     short        heightBits, heightBytes;
  51.     short        width, widthBytes;
  52.     short        rotateWidth;
  53.     short        rotateHeight;
  54.     short        row, column;
  55.     short        texFont,texFace,texMode,texSize;    
  56.     short        err;
  57.     GrafPtr        savePort;
  58.     
  59.     GetPort(&savePort);
  60.     
  61.     texFont = qd.thePort->txFont;
  62.     texFace = qd.thePort->txFace;
  63.     texMode = qd.thePort->txMode;
  64.     texSize = qd.thePort->txSize;
  65.     
  66.     OpenPort( &srcPort);
  67.     SetPort(&srcPort);
  68.     
  69.     TextFont(texFont);    
  70.     TextFace(texFace);    
  71.     TextMode(texMode);    
  72.     TextSize(texSize);
  73.     GetFontInfo( &f);
  74.     
  75.     heightBits = f.ascent + f.descent + f.leading;
  76.     heightBytes = ((heightBits + 7) & 0x7FF8) >> 3;
  77.     width = StringWidth( str);
  78.     widthBytes = ((width + 7) & 0x7FF8) >> 3;
  79.  
  80.     rotateWidth = heightBytes;
  81.     rotateHeight = widthBytes << 3;
  82.     
  83.     srcPort.portBits.baseAddr = NewPtrClear( widthBytes * heightBits);
  84.     if(err = MemError()) return err;
  85.  
  86.     srcPort.portBits.rowBytes = widthBytes;
  87.     SetRect( &srcPort.portBits.bounds, 0, 0, width, heightBits);
  88.  
  89.     destMap->baseAddr = NewPtrClear( rotateWidth * rotateHeight);
  90.     if(err = MemError()) {
  91.         DisposePtr(srcPort.portBits.baseAddr);
  92.         return err;
  93.     }
  94.     
  95.     destMap->rowBytes = rotateWidth;
  96.     SetRect( &destMap->bounds, 0, 0, rotateWidth * 8, rotateHeight);
  97.     
  98.     RectRgn(srcPort.visRgn,&srcPort.portBits.bounds);
  99.     MoveTo( 0, f.ascent);  // change 2.0.1 <CKH> was heightBits
  100.     DrawString( str);
  101.  
  102.     
  103.     /*****************************************/
  104.     /*  Rotate Algorithm                     */
  105.     /*****************************************/
  106.  
  107.     srcPtr = srcPort.portBits.baseAddr;        // Start at the Top,Left
  108.     
  109.     if(direction == counterClockWise)        // Start at the Bottom,Left
  110.         destPtr = destMap->baseAddr + ((rotateHeight-1) * rotateWidth);
  111.     else                                    // Start at the Top,Left
  112.         destPtr = destMap->baseAddr;
  113.                                             
  114.     for( column = 0; column < width; column++)        /* width is in pixels */
  115.     {
  116.         for( row = 0; row < heightBits; row++)        /* height in pixels, too */
  117.         {
  118.         
  119.             if(direction == counterClockWise) {
  120.                 /* We are rotating left ( counter clock-wise) thus */
  121.                 /*  as we search across the top of the horizontal pixel bit map,    */
  122.                 /*    we write down the right edge of the vertical pixel bit map */
  123.  
  124.                 if( *(srcPtr                        /* Begin with the upper left corner */ 
  125.                         + (row*widthBytes)             /* Move down a row */
  126.                         + (column >> 3))            /* Go to the first even byte */
  127.                         & (0x80>>(column & 7)) )    /* calculate a shifted bit position based on the column */
  128.                     {
  129.                         *(destPtr                     /* Begin in the lower left corner */
  130.                             - (column*rotateWidth)     /* Subtract, move up for each row */
  131.                             + (row >> 3))             /* Calculate a bit position for the column */
  132.                             |= (0x80>>(row & 7));    /* Set a bit based on the column */
  133.                     }
  134.             } else {    /* clockWise */
  135.                 /*  We are rotating right (clock-wise) thus */
  136.                 /*  as we search across the top of the horizontal pixel bit map,    */
  137.                 /*    we write down the left edge of the vertical pixel bit map */
  138.                 
  139.                 if( *(srcPtr                        /* Begin with the upper left corner */ 
  140.                         + (row*widthBytes)             /* Move down a row */
  141.                         + (column >> 3))            /* Go to the first even byte */
  142.                         & (0x80>>(column & 7)) )    /* calculate a shifted bit position based on the column */
  143.                     {
  144.                         *(destPtr                     /* Begin in the lower left corner */
  145.                         + (column*rotateWidth) /* Add, move down for each row */
  146.                         - (row >> 3))             /* Calculate a bit position for the column */
  147.                         |= (0x01<<(row & 7));    /* Set a bit based on the column */
  148.                     }
  149.             }
  150.         }
  151.     }
  152.     
  153.     /* clean up the used source BitMap, and restore the ports properly */    
  154.     DisposePtr(srcPort.portBits.baseAddr);
  155.     ClosePort(&srcPort);
  156.     SetPort(savePort);
  157.     
  158.     return noErr;
  159. }
  160.